/*!
 * Component: components/Index/index
 * License: MIT
 * Created: 2020-02-26 16:53
 */

import { checkAuth } from '/store/actions'
import { GlobalData } from "/store/state";

Component({
  /**
   * Component properties
   */
  properties: {
    list: { type: Array, value: [] as DomoCommodity[] },
    currencyname: { type: String, value: '' },
    user: { type: Object, value: {} as DomoUser },
    exchangeRate: { type: String, value: '' },
  },

  /**
   * Component initial data [private]
   */
  data: {
    access: GlobalData.access,
    hasVideo: [],
  },

  observers: {
    list(list = []) {
      const hasVideo = [] as any
      for (let i = 0; i < list.length; i++) {
        const photos = list[i].photos || []
        for (let j = 0; j < photos.length; j++) {
          if (photos[j].video) {
            hasVideo[i] = true
            break
          }
        }
      }
      this.setData({ hasVideo })
    }
  },

  /**
   * lifecycle of component
   */
  lifetimes: {
    attached() {
      this.setData({
        access: GlobalData.access,
      })
    },
  },

  /**
   * Component methods
   */
  methods: {
    async toPay(event: WechatMiniprogram.CustomEvent) {
      await checkAuth()
      /** isOrig===1为原价，反之折扣 */
      const data = event.currentTarget.dataset
      const commodityId = data.id
      wx.navigateTo({
        url: `/mall/pay/pay?commodityId=${commodityId}&orig=${data.isorig}`,
      })
    },
    toDetail(e: any) {
      const {
        currentTarget: {
          dataset: { commodityid },
        },
      } = e

      wx.navigateTo({
        url: `/mall/detail/detail?commodityId=${commodityid}`,
      })
    },
  },
})
